package com.thinkgem.jeesite.common.utils.email;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

public class SendEmailUtils {

	//发送邮件
		public  static void sendEmail(Map<String, Object> paramMap){
			String content = "";	//获取发送邮件内容的模板
			try {
				String filePath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
				filePath = filePath.replace("classes", "views/modules/sendMail");
				filePath=filePath.substring(1); 
				filePath = filePath + "sendEmailModel.xml";	//获取发送邮件内容的XML模板
				Object[] contentParams = (Object[])paramMap.get("content");
				XMLUtils.addDiv(filePath, contentParams.length);//拼接发送内容的样式
				content = XMLUtils.reader(filePath);//读取发送内容
				content = MessageFormat.format(content, contentParams);
				String subject = (String)paramMap.get("subject");
				String receiver = (String)paramMap.get("receiver");
				String cc = (String)paramMap.get("cc"); //抄送地址
				EmailUtils email = new EmailUtils();
				email.send(subject, content, receiver, cc);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//将list转换成String
		public static String listToStr(List list){
			String result = "";
			if(list.size() > 0){
				for(int i=0;i<list.size();i++){
					if(i < list.size() - 1){
						if(list.get(i) != null && !list.get(i).equals("")){
							result = result + list.get(i).toString() + ",";
						}
					}else{
						if(list.get(i) != null && !list.get(i).equals("")){
							result = result + list.get(i);
						}
					}
				}
			}
			return result;
		}
}
